package groovyx

import org.springframework.web.context.ContextLoaderListener
import javax.servlet.ServletContextEvent
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.context.support.WebApplicationContextUtils

/**
 * Spring context loader listener
 *
 * @author linux_china
 */
class SpringContextLoaderListener extends ContextLoaderListener {
  /**
   * spring beans
   */
  public static Map<String, Object> BEANS = [:]

  /**
   * add hook to register customer application context map
   *
   * @param event ServletContextEvent
   */
  @Override
  public void contextInitialized(ServletContextEvent event) {
    super.contextInitialized(event);
    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    applicationContext.beanDefinitionNames.each { BEANS[it] = applicationContext.getBean(it)}
  }
}
